﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using Mulala.Cavan.Application.MainBoundedContext.InformationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.InformationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Presentation.Seedwork;

    public class NoticeController : EntityController<Notice, NoticeSearch, INoticeViewModel, INoticeAppService>, INoticeController
    {
        public NoticeController()
        {
            this.Model = ModelFactory.CreateModel<INoticeViewModel>();
        }

        public override string ControllerName
        {
            get { return ControllerConst.NOTICE; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        [ActionName("recentnotice")]
        [ChildActionOnly]
        public ActionResult GetNoticeByRecent()
        {
            INoticeViewModel noticeViewModel = GetNoticeViewModel(Project.Settings.RecentNoticeAmount);
            return PartialView("~/Views/Notice/_recentNoticePartial.cshtml", noticeViewModel);
        }

        [ActionName("homenotice")]
        [ChildActionOnly]
        public ActionResult GetNoticesByHome()
        {
            INoticeViewModel noticeViewModel = GetNoticeViewModel(Project.Settings.HomeNoticeAmount);
            return PartialView("~/Views/Notice/_homeNoticePartial.cshtml", noticeViewModel);
        }

        private INoticeViewModel GetNoticeViewModel(int top)
        {
            Dictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("CreateTime", true);

            NoticeSearch noticeSearch = NoticeSearchFactory.CreateNoticeSearch(orderBy, 1, top);
            INoticeViewModel noticeViewModel = ModelFactory.CreateModel<INoticeViewModel>();
            IList<Notice> notices = this.AppService.GetBySearch(noticeSearch).ToList();
            noticeViewModel.Notices = notices;

            return noticeViewModel;
        }

        [ActionName("list")]
        public ActionResult GetNotices(int cp)
        {
            Dictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("CreateTime", true);

            NoticeSearch noticeSearch = NoticeSearchFactory.CreateNoticeSearch(orderBy, cp, Project.Settings.MaxRowsPerPage);
            long total = 0;
            IList<Notice> notices = this.AppService.GetBySearch(noticeSearch, out total).ToList();
            noticeSearch.TotalRowCount = total;

            INoticeViewModel noticeViewModel = ModelFactory.CreateModel<INoticeViewModel>();
            noticeViewModel.SearchParam = noticeSearch;
            noticeViewModel.Notices = notices;

            if (this.Model.MasterModel.CurrentUserModel.UserRole == Domain.MainBoundedContext.SecurityModule.SecurityIdentifier.Role.Corporation)
            {
                return View("~/Views/Notice/NoticeForCorporation.cshtml", noticeViewModel);
            }
            else if (this.Model.MasterModel.CurrentUserModel.UserRole == Domain.MainBoundedContext.SecurityModule.SecurityIdentifier.Role.Corporation)
            {
                return View("~/Views/Notice/NoticeForNanny.cshtml", noticeViewModel);
            }
            else if (this.Model.MasterModel.CurrentUserModel.UserRole == Domain.MainBoundedContext.SecurityModule.SecurityIdentifier.Role.Corporation)
            {
                return View("~/Views/Notice/NoticeForProprietor.cshtml", noticeViewModel);
            }
            return View(noticeViewModel);
        }
    }
}
